from .. import db
from datetime import datetime

class OilSupplyRelation(db.Model):
    """油料供应关系模型，描述两个设施之间的供应关系"""
    __tablename__ = 'oil_supply_relations'

    id = db.Column(db.Integer, primary_key=True)
    source_id = db.Column(db.Integer, db.ForeignKey('oil_facilities.id'), nullable=False)
    target_id = db.Column(db.Integer, db.ForeignKey('oil_facilities.id'), nullable=False)
    supply_capacity = db.Column(db.Float, nullable=False)  # 供应能力（万吨/年）
    transportation_type = db.Column(db.String(50), nullable=True)  # 运输方式：pipeline(管道), railway(铁路), road(公路)
    distance = db.Column(db.Float, nullable=True)  # 距离（公里）
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

    def __repr__(self):
        return f'<OilSupplyRelation {self.id}: {self.source_id}->{self.target_id}>'

    def to_dict(self):
        """转换为字典表示"""
        return {
            'id': self.id,
            'source_id': self.source_id,
            'target_id': self.target_id,
            'supply_capacity': self.supply_capacity,
            'transportation_type': self.transportation_type,
            'distance': self.distance,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        } 